package com.acwing.partition3;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2022/2/11 15:49
 */
public class AC237程序自动分析 {

    private static final int N = 200010;
    private static int[] parent = new int[N];
    private static Map<Integer, Integer> map = new HashMap<>(N);
    private static List<int[]> list1 = new ArrayList<>(N), list2 = new ArrayList<>(N);

    private static int n = 0, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int t = Integer.parseInt(reader.readLine());
        while (t-- > 0) {
            n = Integer.parseInt(reader.readLine());
            for (int i = 1; i <= 2 * n; i++) parent[i] = i;
            idx = 0;
            map.clear();
            list1.clear();
            list2.clear();
            for (int i = 0; i < n; i++) {
                String[] ss = reader.readLine().split(" ");
                int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), c = Integer.parseInt(ss[2]);
                if (!map.containsKey(a)) map.put(a, ++idx);
                if (!map.containsKey(b)) map.put(b, ++idx);
                a = map.get(a);
                b = map.get(b);
                if (c == 1) list1.add(new int[]{a, b});
                else list2.add(new int[]{a, b});
            }
            if (resolve()) writer.write("YES\n");
            else writer.write("NO\n");
        }
        writer.flush();
    }

    private static boolean resolve() {
        for (int[] pair : list1) {
            int a = pair[0], b = pair[1];
            parent[find(a)] = parent[find(b)];
        }
        for (int[] pair : list2) {
            int a = pair[0], b = pair[1];
            if (find(a) == find(b)) return false;
        }
        return true;
    }

    private static int find(int x) {
        if (x != parent[x]) parent[x] = find(parent[x]);
        return parent[x];
    }
}
